﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using LightSwitchApplication.UserCode;
namespace LightSwitchApplication
{
    public partial class SalesOrdersListDetail
    {
        private DialogHelper dialogHelper;
        private DialogHelper dialogVAT;
        partial void SalesOrderListAddAndEditNew_CanExecute(ref bool result)
        {
            // Write your code here.
            result = dialogHelper.CanAdd(); 
        }

        partial void SalesOrderListAddAndEditNew_Execute()
        {
            // Write your code here.
            dialogHelper.AddEntity(); 
        }

        partial void SalesOrdersListDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            dialogHelper = new DialogHelper(this.SalesOrders, "ModalAdd",500,-1);
            dialogVAT = new DialogHelper(this.SalesOrders, "dialogVAT", 500, -1); 
        }

        partial void SalesOrdersListDetail_Created()
        {
            // Write your code here.
            dialogHelper.InitializeUI();
            dialogVAT.InitializeUI();
            dialogVAT.IsAllwayUpdate = true;
        }

        partial void AddOK_Execute()
        {
            // Write your code here.
            dialogHelper.DialogOK(); 
        }

        partial void AddCancel_Execute()
        {
            // Write your code here.
            dialogHelper.DialogCancel(); 
        }

        partial void ShowVAT_Execute()
        {
            // Write your code here.
            dialogVAT.EditSelectedEntity();
        }

        partial void ShowVAT_CanExecute(ref bool result)
        {
            // Write your code here.
            result = dialogVAT.CanEditSelected();
             
        }

        partial void SalesOrdersListDetail_Activated()
        {
            // Write your code here.
            if (IsOpenAndNew !=null && IsOpenAndNew.Value)
            {
                SalesOrderListAddAndEditNew_Execute();
                IsOpenAndNew = false;
            }
        }

        partial void Reciept_Execute()
        {
            // Write your code here.
            // Write your code here.
            if (this.SalesOrders == null || this.SalesOrders.SelectedItem == null)
            {
                System.Windows.MessageBoxResult result = this.ShowMessageBox("Vui lòng chọn một chứng từ", "Thông báo", MessageBoxOption.Ok);
                return;
            }

            if (IsDataChanged())
            {
                System.Windows.MessageBoxResult result = this.ShowMessageBox("Dữ liệu đã thay đổi, Bạn cần phải lưu lại", "Thông báo", MessageBoxOption.Ok);
                return;
            }

            if (this.SalesOrders.SelectedItem.CustsVend == null || this.SalesOrders.SelectedItem.CustsVend.CustVendID == null)
            {
                System.Windows.MessageBoxResult result = this.ShowMessageBox("Vui lòng chọn một nhà cung cấp", "Thông báo", MessageBoxOption.Ok);
                return;
            }

            Application.ShowRecieptListByRefListDetail(this.SalesOrders.SelectedItem.DocumentID, false); 
        }

        private bool IsDataChanged()
        {
            if (this.SalesOrders.SelectedItem.Details.EntityState != EntityState.Unchanged) return true;
            foreach (var item in this.SalesOrderDetails)
            {
                if (item.Details.EntityState != EntityState.Unchanged) return true; 
            }
            return false;
        }
    }
}
